#include "RingQueue.hpp"

struct ThreadData{
	Ringqueue<int>* rq;
	std::string threadname;
};

void * Productor(void* args)
{
	ThreadData* td = static_cast<ThreadData*>(args);
	Ringqueue<int>* rq = td->rq;
	std::string name = td->threadname;
	// sleep(1);
	while(true)
	{
		// std::cout << "I am a Productor" << std::endl;
		int data = rand() % 10 + 1;
		rq->push(data);
		std::cout << "I am a Productor,prdocute done,date : " << data << " who : " << name << std::endl;
	}
}

void * Consumer(void* args)
{
	ThreadData* td = static_cast<ThreadData*>(args);
	Ringqueue<int>* rq = td->rq;
	std::string name = td->threadname;
	// Ringqueue<int>* rq = static_cast<Ringqueue<int>*>(args);
	while(1)
	{
		// std::cout << "I am a Comsumer" << std::endl;
		int data = 0;
		rq->pop(&data);
		std::cout << "I am a Consumer,consume done,date : " << data << " who : " << name << std::endl;
		sleep(1);
	}
}

int main()
{
	srand(time(nullptr) ^ getpid());

	Ringqueue<int> * rq = new Ringqueue<int>(50);

	pthread_t c[5],p[3];

	for(int i = 0;i < 3;i++)
	{
		ThreadData* td = new ThreadData();
		td->rq = rq;
		td->threadname = "thread-" + std::to_string(i);
		pthread_create(p+i,nullptr,Productor,td);
	}
	for(int i = 0;i < 5;i++)
	{
		ThreadData* td = new ThreadData();
		td->rq = rq;
		td->threadname = "thread-" + std::to_string(i);
		pthread_create(c+i,nullptr,Consumer,td);
	}
	for(int i = 0;i < 3;i++)
	{
		pthread_join(p[i],nullptr);
	}
	for(int i = 0;i < 5;i++)
	{
		pthread_join(c[i],nullptr);
	}

	return 0;
}